<template>
  <!-- 检验规则维护（按品号）-首检的弹窗 -->
  <p-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">
        <p-row>
          <p-col :span="12">
            <p-form-item label="一级工序名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                class="ant-input"
                v-decorator="[ 'processOneName', validatorRules.processOneName]"
                placeholder="请输入一级工序名称"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="一级工序编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <f-popup
                contextPath="mes-craft"
                v-decorator="['processOneCode', validatorRules.processOneCode]"
                :trigger-change="true"
                org-fields="process_code,process_name,process_twocode,process_twoname"
                dest-fields="process_one_code,process_one_name,process_two_code,process_two_name"
                code="mes_route"
                :add="this.url.add"
                isMultiSelect="popup"
                @oNvisible="oNvisible"
                :listaip="this.listaip"
                :mainTableID="this.mainTableID"
                @callback="popupCallback"
              />
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="二级工序名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                class="ant-input"
                v-decorator="[ 'processTwoName', validatorRules.processTwoName]"
                placeholder="请输入二级工序名称"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="二级工序编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                class="ant-input"
                v-decorator="[ 'processTwoCode', validatorRules.processTwoCode]"
                placeholder="请输入二级工序编码"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="是否首检" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="radio"
                v-decorator="['isFirstInspect', validatorRules.isFirstInspect]"
                :trigger-change="true"
                dictCode="ba_yn"
                placeholder="请选择是否首检"
              />
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="radio"
                v-decorator="['idDate', validatorRules.idDate]"
                :trigger-change="true"
                dictCode="ba_yn"
                placeholder="请选择日期"
              />
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="整机品号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="radio"
                v-decorator="['isMachineItem', validatorRules.isMachineItem]"
                :trigger-change="true"
                dictCode="ba_yn"
                placeholder="请选择整机品号"
              />
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="线别" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="radio"
                v-decorator="['isLine', validatorRules.isLine]"
                :trigger-change="true"
                dictCode="ba_yn"
                placeholder="请选择线别"
              />
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="工单" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="radio"
                v-decorator="['isWorkorder', validatorRules.isWorkorder]"
                :trigger-change="true"
                dictCode="ba_yn"
                placeholder="请选择工单"
              />
            </p-form-item>
          </p-col>
          <p-col :span="12">
            <p-form-item label="机台" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="radio"
                v-decorator="['isMachine', validatorRules.isMachine]"
                :trigger-change="true"
                dictCode="ba_yn"
                placeholder="请选择机台"
              />
            </p-form-item>
          </p-col>
        </p-row>
      </p-form>
    </p-spin>
  </p-modal>
</template>

<script>
import { httpAction } from "@/api/manage";
import { SubmitOnchangMixin } from "@/mixins/SubmitOnchangMixin";
import pick from "lodash.pick";
import JDictSelectTag from "@/components/dict/JDictSelectTag";

export default {
  name: "MesQcInspectRuleBmcFinspModal",
  mixins: [SubmitOnchangMixin],
  components: {
    JDictSelectTag,
  },
  props: {
    mainId: {
      type: String,
      required: false,
      default: "",
    },
    listaip: {
      type: String,
      default: "",
    },
  },
  watch: {
    mainId: {
      deep: true,
      handler(val) {
        this.mainTableID["mesInspectRuleBymcodeId"] = val;
      },
    },
  },
  data() {
    return {
      form: this.$form.createForm(this),
      title: "操作",
      mainTableID: {},
      width: 1200,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 8 },
      },
      visible: false,
      model: {},
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },

      confirmLoading: false,
      validatorRules: {
        processOneName: {
          rules: [{ required: false, message: "请输入一级工序名称!" }],
        },
        processOneCode: {
          rules: [{ required: false, message: "请输入一级工序编码!" }],
        },
        processTwoName: {
          rules: [{ required: false, message: "请输入二级工序名称!" }],
        },
        processTwoCode: {
          rules: [{ required: false, message: "请输入二级工序编码!" }],
        },
        isFirstInspect: {},
        idDate: {},
        isMachineItem: {},
        isLine: {},
        isWorkorder: {},
        isMachine: {},
      },
      url: {
        add: "/mes-quality/mesQcInspectRuleBymcode/addMesQcInspectRuleBmcFinsp",
        edit:
          "/mes-quality/mesQcInspectRuleBymcode/editMesQcInspectRuleBmcFinsp",
      },
    };
  },
  created() {},
  methods: {
    add() {
      this.edit({});
    },
    edit(record) {
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            "createBy",
            "createTime",
            "updateBy",
            "updateTime",
            "remark",
            "timestamp",
            "companyId",
            "factoryId",
            "clientId",
            "substituteId",
            "flag",
            "mesInspectRuleBymcodeId",
            "processOneName",
            "processOneCode",
            "processOneId",
            "processTwoName",
            "processTwoCode",
            "processTwoId",
            "isFirstInspect",
            "idDate",
            "isMachineItem",
            "isLine",
            "isWorkorder",
            "isMachine"
          )
        );
      });
    },
    close() {
      this.$emit("close");
      this.visible = false;
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let httpurl = "";
          let method = "";
          let formData = Object.assign(this.model, values);
          this.formData = Object.assign(this.model, values);
          formData["mesInspectRuleBymcodeId"] = this.mainId;
          this.formData["mesInspectRuleBymcodeId"] = this.mainId;
          //新增触发
          if (!this.model.id) {
            httpurl += this.url.add;
            method = "post";
            if (this.EnhanceJS.afterAdd) {
              //新增之前
              let Obj = eval(this.EnhanceJS.afterAdd);
              new Obj(this);
            } else {
              console.log("表单提交数据", formData);
              that.modalFrom(httpurl, formData, method);
            }
          } else {
            //编辑触发
            httpurl += this.url.edit;
            method = "put";
            if (this.EnhanceJS.afterEdit) {
              //编辑之前
              let Obj = eval(this.EnhanceJS.afterEdit);
              new Obj(this);
            } else {
              console.log("表单提交数据", formData);
              that.modalFrom(httpurl, formData, method);
            }
          }
        }
      });
    },
    handleCancel() {
      this.close();
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(
          row,
          "createBy",
          "createTime",
          "updateBy",
          "updateTime",
          "remark",
          "timestamp",
          "companyId",
          "factoryId",
          "clientId",
          "substituteId",
          "flag",
          "mesInspectRuleBymcodeId",
          "processOneName",
          "processOneCode",
          "processOneId",
          "processTwoName",
          "processTwoCode",
          "processTwoId",
          "isFirstInspect",
          "idDate",
          "isMachineItem",
          "isLine",
          "isWorkorder",
          "isMachine"
        )
      );
    },
  },
};
</script>
